/**
 * Created by Joe on 2017/7/5 0005.
 */
var Message = {
    fields: {
        selects: ["status", "isRead"], //下拉框
        radios: [],//单选框
        checkboxs: [],//复选款
        textareas: ["content"],//多行文本框
        selfDefine: []
    }
};

$(document).ready(function () {

    var table = $('#DataTables_message').DataTable({
        "paging": true,
        "searching": true,
        "info": true,
        "ordering": true,
        "language": {
            "url": "../vendor/datatables/zh-cn.json"
        }
    });

    $('#btn-add-message').on('click', function () {
        Utils.clearForm("#new-message-form");
        Utils.formWritable("#new-message-form");
        $("#new-message-modal").modal("toggle");
    });
    $('.save-new-message').on('click', function () {
        requestCreateNew();
    });
    $('#btn-query').on('click', function () {
        var userName = $("#query-form").find("input[name='userName']").val();
        var title = $("#query-form").find("input[name='title']").val();
        var content = $("#query-form").find("input[name='content']").val();
        clearAlerts("query-form");
        if ($.trim(userName) == '' && $.trim(title) == '' && $.trim(content) == '') {
            alerts("query-form", 2, "请输入查询条件");
            return;
        }
        $("#DataTables_message").DataTable().rows().remove().draw();
        var url = /*[[@{/queryBy}]]*/ 'queryBy';
        var param = $("#query-form").serialize();
        Utils.ajaxSubmit(url, param, function (data) {
            if (data.result == "success") {
                addTableRow(data.content.list, "#DataTables_message");
            }
        })
    });
    $('#DataTables_message').on('click', '.btn-detail', function () {
        requestDetail($(this).siblings("input").val());
    });
});


function requestCreateNew() {
    var url = /*[[@{/createMessage}]]*/ 'createMessage';
    var param = $("#new-message-form").serialize();
    Utils.ajaxSubmit(url, param).done(function (data) {
        if (data.result == "success") {
            addTableSingleRow(data.content, "#DataTables_message");
            $("#new-message-modal").modal("toggle");
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestDetail(messageId) {
    var url = /*[[@{/findDetail}]]*/ 'findDetail';
    Utils.clearForm("#message-form");
    $("#real_img").empty();
    Utils.ajaxSubmit(url + "/" + messageId, {}, function (data) {
        console.log("data ： ", data);
        if (data.result == "success") {
            data.content.typeDesc = data.content.typeDesc == '1' ? "系统消息" : "业务消息";
            data.content.category = '文字';
            data.content.sender = "系统";
            Utils.formReadOnly("#message-form");
            Utils.fillForms(data.content, Message.fields);
            $("#message-modal").modal("toggle");
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
/**添加table行*/
function addTableRow(messageList, targetEle) {
    $.each(messageList, function (k, v) {
        addTableSingleRow(v, targetEle);
    });
}
function addTableSingleRow(v, targetEle) {
    v.type = v.type == '1' ? "系统消息" : "业务消息";
    v.status = v.status == '0' ? "未发送" : "已发送";
    v.isRead = v.isRead == '0' ? "未读" : "已读";
    var btnRemoveEle = '<input type="hidden" value="' + v.id + '"> <a class="btn btn-primary btn-detail" >查看明细</a>';
    // btnRemoveEle = btnRemoveEle.concat('&nbsp;&nbsp;&nbsp;<a class="btn btn-danger btn-detail-delete">删除</a>');
    $(targetEle).dataTable().fnAddData([v.userName, v.type, '文字', v.title, v.content, v.status, v.isRead, btnRemoveEle]);
}
